<?php
    /**
    * Controller is the customized base controller class.
    * All controller classes for this application should extend from this base class.
    */
    class Controller extends CController
    {
        /**
        * @var string the default layout for the controller view. Defaults to '//layouts/column1',
        * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
        */
        public $layout='//layouts/main';
        /**
        * @var array context menu items. This property will be assigned to {@link CMenu::items}.
        */
        public $menu=array();
        /**
        * @var array the breadcrumbs of the current page. The value of this property will
        * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
        * for more details on how to specify this property.
        */
        public $breadcrumbs=array();

        public $pageTitle;
        public $pageDesc;
        public $pageKeywords;

        public $siteName;
        public $siteSlogan;
        public $siteDesc;

        public $widgets = array();

        public $baseUrl;
        public $themeUrl;
        public $domain;

        public $ogUrl;
        public $ogImage;         
                            
        public $cats;
        public $keyword_search;
        public $menu_selected;
        
        public $manager;
        
        public $detect;
        public $useMoxie = 1;
        
        public $lang = 'vi';
        public $other = array();
        public $googleMapURI;
        
        public $smsNoticeData = array();

        public function actions()
        {
            return array(
                'captcha'=> Yii::app()->params->captcha['action'],
            );
        }

        public function init(){
            
            // detect ie <= 8
            if(preg_match('/(?i)msie [5-7]/', $_SERVER['HTTP_USER_AGENT'])){
                $this->renderPartial('//web/browsers');
                Yii::app()->end();
            }
            
            $this->googleMapURI = "http://maps.googleapis.com/maps/api/js?key=".Myext::getRandomArray(Yii::app()->params->google['apiKey'])."&sensor=true&libraries=places&language=vi";
            $lang = Yii::app()->request->getQuery('lang');
            if(!in_array($lang, array('vi', 'en'))){
                $lang = 'vi';
            }
            $this->lang = $lang;
            Yii::app()->setLanguage($lang);

            Yii::app()->request->cookies['lang'] = new CHttpCookie('lang', $lang); 
            // set & detect cat by lang
            $this->cats = PostCat::model()->findAll();
            foreach($this->cats as & $cat){
                $cat->detectByLang();
            }


            $this->other['lang'] = $this->lang == 'en' ? 'vi' : 'en';
            
//            Yii::import('ext.Mobile_Detect');
//            $this->detect = new Mobile_Detect();
            
            // register files
            Yii::app()->clientScript->registerCoreScript('jquery.ui');
            //Yii::app()->clientScript->registerCssFile(Yii::app()->clientScript->getCoreScriptUrl().'/jui/css/base/jquery-ui.css');
//            Yii::app()->clientScript->registerCssFile('/css/jquery-ui-mac/style.css');


            // register class paths for extension captcha extended
            Yii::$classMap = array_merge( Yii::$classMap, array(
                'CaptchaExtendedAction' => Yii::getPathOfAlias('ext.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedAction.php',
                'CaptchaExtendedValidator' => Yii::getPathOfAlias('ext.captchaExtended').DIRECTORY_SEPARATOR.'CaptchaExtendedValidator.php'
            ));

            $this->siteName = Yii::app()->params->site['name'];
            $this->siteSlogan = Yii::app()->params->site['slogan'];
            $this->siteDesc = Yii::app()->params->site['desc'];
            
            $this->pageTitle = $this->siteName. ' - '.$this->siteSlogan;
            $this->pageDesc = $this->siteDesc;
            

        
            $this->baseUrl = Yii::app()->request->getBaseUrl(TRUE);
            $this->themeUrl = Yii::app()->theme->baseUrl;
            $this->domain = preg_replace('{https?://(.+)}', '$1', $this->baseUrl);
            
            $this->cats = PostCat::model()->findAll(new CDbCriteria(array('order' => '`index`')));
            $this->manager = Yii::app()->adminUser->manager;
            
            $this->smsNoticeData = SmsNotice::model()->getLastNotice();
            

        }
        
        function t($s, $params=array()){
            return Yii::t('app', $s, $params);
        } 
}